<?php
function __autoload($class_name) {
        include 'class.' . strtolower($class_name) . '.php';
    }

class UserDao {    

    public function getUser(User $user){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT id_user,id_parent_user,nip,id_kewenangan FROM t_users WHERE nip='$user->nip'";
        //print_r($query);
        $result=$con->prepare($query);
        $result->execute();
        //print_r($result->rowCount());
        if($result->rowCount()==1){
            return $result->fetch();
        }

        return false;
    }

    public function cekUser($nip){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT * FROM t_users WHERE nip='$nip'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function cekAktif($id_user){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT status FROM t_users WHERE id_user='$id_user'";

        $result=$con->prepare($query);
        $result->execute();
 
        if($result->rowCount()==1){
            $data=$result->fetch();
            if($data['status']=='0'){
                return false;
            }else {
                return true;
            }
        }

        return false;
    }

    public function getUserById(User $user){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT id_user,id_parent_user,nip,password,id_kewenangan FROM t_users WHERE id_user='$user->id_user'";
        //print_r($query);
        $result=$con->prepare($query);
        $result->execute();
 
        if($result->rowCount()==1){
            return $result->fetch();
        }

        return false;
    }

    public function getNip($id_user){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT nip FROM t_users WHERE id_user='$id_user'";
        //print_r($query);
        $result=$con->prepare($query);
        $result->execute();
 
        if($result->rowCount()==1){
            $data=$result->fetch();
            return $data['nip'];
        }

        return false;
    }

    public function getAllUser($id_parent, $id_kewenangan){
        $db=Database::getInstance();
        $con=$db->getConnection(1);
        $query="SELECT id_user,id_parent_user,nip,id_kewenangan,status FROM t_users ";
        if($id_kewenangan!=1){
            $query.=" WHERE id_parent_user='$id_parent' ";
        }
        //$query .= " id_user<>'$id_parent'";
        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()>=1){
            return $result->fetchAll();
        }

        return false;
    }

    public function loginUser(User $user){
        $password=md5($user->password);

        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="SELECT nip FROM t_users WHERE nip='$user->nip' and password='$password'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount() ==1){
            return true;
        }

        return false;

    }

    public function createUser(User $user){
        $db= Database::getInstance();
        $con=$db->getConnection(1);

        $password=md5($user->password);
        $date_created=date('Y-m-d H:i:s');


        $query="INSERT INTO t_users(nip,password,id_parent_user,id_kewenangan,status,date_created,user_created) ";
        $query.=" VALUES('$user->nip','$password','$user->id_parent_user','$user->id_kewenangan','$user->status','$date_created','$user->user_creating')";

        //$data=array($user->nip,$password,$user->id_parent_user,$user->id_kewenangan,$date_created,$user->user_creating);

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function deleteUser($id_user){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="DELETE FROM t_users WHERE id_user='$id_user'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function aktivasiUser($id_user){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $query="UPDATE t_users SET status='1' WHERE id_user='$id_user'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

    public function updateUser(User $user){
        $db=Database::getInstance();
        $con=$db->getConnection(1);

        $password=md5($user->password);
        $date_updated=date('Y-m-d H:i:s');

        $query="UPDATE t_users SET nip='$user->nip', password='$password',id_kewenangan='$user->id_kewenangan',
             date_updated='$date_updated', user_updating='$user->id_parent' WHERE id_user='$user->id_user'";

        $result=$con->prepare($query);
        $result->execute();

        if($result->rowCount()==1){
            return true;
        }

        return false;
    }

}

class User {

    var $id_user;
    var $nip;
    var $username;
    var $password;
    var $id_kewenangan;
    var $id_parent;
    var $status;

}
?>